#coding=utf-8
#将指定投产日期的开发任务改为另一个时间，同时限定kfzx
#命令参数 kfzx date dst_date
import sys

from uliweb.orm import get_model, set_dispatch_send, Begin, Commit, Rollback

set_dispatch_send(False)

def call(args, options, global_options):
    from uliweb.core.SimpleFrame import get_apps, get_app_dir, Dispatcher

    if len(args) != 4:
        print "Usage: uliweb call change_version_date kfzx_code from_date(yyyy-mm-dd) to_date(yyyy-mm-dd)"
        sys.exit(1)
        
    app = Dispatcher(apps_dir=global_options.apps_dir, start=False)
    Begin()
    try:
        process(args[1:], options, global_options)
        Commit()
    except:
        Rollback()
        import traceback
        traceback.print_exc()
        
def process(args, options, global_options):
    import datetime
    from uliweb.utils.date import to_date
    
    P = get_model('plan')
    PE = get_model('planedit')
    T = get_model('tasks_develop')
    T.modified_user.default = None
    
    from_date = to_date(datetime.datetime.strptime(args[1], '%Y-%m-%d'))
    to_date = to_date(datetime.datetime.strptime(args[2], '%Y-%m-%d'))
    
    n = 0
    for t in T.filter(T.c.kfzx==int(args[0])).filter(T.c.version_date==from_date).filter(T.c.stage!='FN').filter(T.c.status.in_(['1', '2'])):
        P.filter(P.c.task==t.id).filter(P.c.idn=='1090').filter(P.c.end_date<to_date).update(end_date=to_date)
        PE.filter(PE.c.task==t.id).filter(PE.c.idn=='1090').filter(PE.c.end_date<to_date).update(end_date=to_date)
#        t.version_date = to_date
#        if t.end_time <= from_date:
#            t.end_time = to_date
#        t.save()
        n += 1
    print 'Total: %d' % n
    
